/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ortus.onlinescrapper.themoviedb;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *
 * @author jphipps
 */
public class GoogleProvider extends ortus.ortusvars {

    private String ProviderName = "Google";
    
    public String GetProviderName() {
        return ProviderName;
    }

    public HashMap<String,String> Search(String title, int limit) {
        int search_count = 0;
        long t0 = System.currentTimeMillis();
        ortus.api.DebugLog(TRACE, ProviderName + " : Search for title: " + CleanName(title));
        HashMap<String,String> resultTitles = new HashMap<String,String>();

        StringBuffer sb = new StringBuffer();
         try {
                // Send data
                URL url = new URL("http://www.google.com/search?q=" + URLEncoder.encode(CleanName(title)));
                HttpURLConnection conn = (HttpURLConnection)url.openConnection();
                conn.setInstanceFollowRedirects(true);
                conn.setRequestProperty("User-Agent","Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.1) Gecko/2008072820 Firefox/3.0.1");
                conn.connect();

                if ( conn.getResponseCode() != conn.HTTP_OK) {
                    ortus.api.DebugLog(ERROR, ProviderName + " : HTTP: " + conn.getResponseCode());
                    if ( conn.getResponseCode() == conn.HTTP_NOT_FOUND ) {
                        return null;
                    }
                    return null;
                }

                BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                String line;
                while ((line = rd.readLine()) != null) {
                    sb.append(line);
                }
                rd.close();
            } catch (Exception e) {
                System.out.println("HTTP Exception: " + e);
                return null;
            }

            List<String> y = getH3(sb.toString());

            for( String html : y) {
//                Pattern pattern = Pattern.compile("http://www.imdb.com/title/([^/]+)/");
//                Matcher matcher = pattern.matcher(html);
//                if( matcher.find()) {
//                    ortus.api.DebugLog(TRACE2,"sending: " + html);
                    List<String> result = getTitleAhref(html);
                    if ( result.size() > 0) {
                        search_count++;
                        ortus.api.DebugLog(TRACE, ProviderName + " :  Found: " + ortus.util.string.decodeString(result.get(1)) + " id: " + result.get(0));
                        resultTitles.put(ortus.util.string.decodeString(result.get(1)), "imdb:" + result.get(0));
//                    } else {
//                        ortus.api.DebugLog(TRACE2,"result size:" + result.size());
//                        for( String z : result) {
//                            ortus.api.DebugLog(TRACE2,"result: " + z);
 //                       }
                    }
                    if ( search_count > limit)
                        break;
//                }
            }
            long ttotal = System.currentTimeMillis() - t0;
            ortus.api.DebugLog(TRACE, ProviderName + " : Search returned " + resultTitles.size() + " in " + ttotal + " ms");
            return resultTitles;
      }

       public String CleanName(String title) {
        String cleantitle = null;

        cleantitle = title.replace("_WS", "");
        cleantitle = cleantitle.replaceAll("_LB","");
        cleantitle = cleantitle.replaceAll("_43","");
        cleantitle = cleantitle.replaceAll("_16X9LB_NA","");
        cleantitle = cleantitle.replaceAll("_16X9LB","");
        cleantitle = cleantitle.replaceAll("_4X3FF","");
        cleantitle = cleantitle.replaceAll("_169","");
        cleantitle = cleantitle.replaceAll("_4X3LB","");
        cleantitle = cleantitle.replaceAll("UNRATED", "");
        cleantitle = cleantitle.replaceAll("RATED", "");
        cleantitle = cleantitle.replaceAll("_", " ");
        cleantitle = cleantitle.replaceAll("\\.","");
        cleantitle = cleantitle.replaceAll("16X9","");

        cleantitle = cleantitle.toLowerCase();

        Pattern pattern = Pattern.compile("\\(.*\\d+.*\\)");
        Matcher matcher = pattern.matcher(cleantitle);
        StringBuffer sb = new StringBuffer(cleantitle.length());
        while (matcher.find())
           matcher.appendReplacement(sb, Matcher.quoteReplacement(""));

        matcher.appendTail(sb);
        cleantitle = sb.toString();
//        System.out.println("title: " + cleantitle);

        return cleantitle;
    }

    private static List<String> getH3(String val) {
        List<String> result = new ArrayList<String>();
        Pattern pattern = Pattern.compile("\\<h3 class=r\\>(.*?)\\</h3\\>");
        Matcher matcher = pattern.matcher(val);
        while (matcher.find()) {
            for ( int x = 1; x <= matcher.groupCount(); x++) {
                result.add(matcher.group(x));
            }
        }
        return result;

    }
    
   private static List<String> getTitleAhref(String val) {
        List<String> result = new ArrayList<String>();
        Pattern pattern = Pattern.compile("\\<a href=\"http://www.imdb.com/title/([^/]+)/\"(.*)\\</a\\>");
        Matcher matcher = pattern.matcher(val);
        while (matcher.find()) {
            String title = "";
            result.add(matcher.group(1));
            Pattern pat = Pattern.compile("\\<em\\>([^\\<]+)\\</em\\>");
            Matcher mat = pat.matcher(matcher.group(2));
            while( mat.find() ) {
                title+= mat.group(1);
            }
	    if ( title.isEmpty()) {
		pat = Pattern.compile("\\>([^\\(]+)\\(");
                mat = pat.matcher(matcher.group(2));
                while( mat.find() ) {
                   title+= mat.group(1);
                }
	    }
            result.add(title);
        }

        return result;

    }

}